import Util.Utils;
import algorithm.ACO;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author letere
 * @create 2020-12-24 11:59
 */
public class Main {

    private static final Integer Max_Time = 100;
    private static final Integer Max_Cost = 3000;

    public static void main(String[] args) {


        test();
//        test1();
    }

    public static void test() {

        //删除城市
        List<Integer> citys = Utils.getCitys();



        double[][] pheromoneMatrix = Utils.getPheromoneMatrix(1.0);
        int[][] distanceMatrix = Utils.getDistanceMatrix();
        int [][] timeMartix = Utils.getTimeMatrix();

        ACO aco = new ACO(citys, pheromoneMatrix, distanceMatrix,timeMartix, 150, 72);

//        aco.run();
//        System.out.println(aco.getBestRoute());
//        //[26, 4, 35, 45, 10, 24, 42, 5, 48, 39, 32, 21, 13, 25, 14, 23, 11, 12, 33, 46, 15, 40, 9, 1, 8, 38, 31, 44, 18, 7, 28, 36, 30, 6, 37, 19, 27, 43, 17, 20, 47, 3, 22, 16, 41, 34, 29, 2, 26]
//        System.out.println(aco.getBestLength());
//        //35433

        ArrayList<ACO> ans = new ArrayList<>();
        List<Integer> copyCitys1 = DeepClone.deepCopy(citys);
        //初始，判断遍历citys是否满足条件
        if (aco.getWholeTime() < Max_Time) {
            ans.add(aco);
        }
        if (ans.isEmpty()) {
            //不满足条件，需要删除节点
            while (copyCitys1.size() >= 1) { //每次删除一个节点
                int curLength = 10000000; //定义一个较大数
                int deletedCity = 0;
                int curWholeTime = 0;
                ArrayList<Integer> timeList = new ArrayList<>();
                HashMap<Integer, ACO> acoHashMap = new HashMap<>();

                //从前往后遍历citys，每次选择一个节点删除，找出最优的删除方法
                for (int i = 1; i < copyCitys1.size(); i++) {
                    List<Integer> copyCitys2 = DeepClone.deepCopy(copyCitys1);
                    copyCitys2.remove(i);
                    //重新计算aco
                    aco = new ACO(copyCitys2, pheromoneMatrix, distanceMatrix,timeMartix, 150, 72);
                    acoHashMap.put(i,aco);
                    //判断是否满足条件
                    if (aco.getWholeTime() > Max_Time) { //如果不满足条件，进入下一次循环
                        continue;
                    } else {
                        if (aco.getBestLength() < curLength) { //如果所删除节点对应的路径长度小于上一个删除节点对应的路径长度，则替换
                            curLength = aco.getBestLength();
                            deletedCity = i;
                            curWholeTime = aco.getWholeTime();
                        }
                    }
                }
                timeList.add(deletedCity);
                timeList.add(curWholeTime);

                //如果timelist中元素不为0，说明删除一个节点有效
                if (timeList.get(0) != 0) {
                    ACO aco1 = acoHashMap.get(timeList.get(0));
                    ans.add(aco1); //这里可以直接输出方案了
                } else { //说明删除一个节点还不行，需要继续删除
                    //找出其中最短路径方案所删除的节点
                    int key = 0;
                    int tempMinLength = 10000000; //定义一个较大数
                    Set<Integer> keySet = acoHashMap.keySet();
                    for (Integer key1 : keySet) {
                        if (acoHashMap.get(key1).getBestLength() < tempMinLength) {
                            tempMinLength = acoHashMap.get(key1).getBestLength();
                            key = key1; //key为最短路径对应的删除节点
                        }
                    }

                    //删除key节点，并在此基础上重复上述步骤，删除节点
                    copyCitys1.remove(key);
                }
            }
        }
        System.out.print(" route is :" + aco.getBestRoute() + ",  ");
        System.out.print(" length is :" + aco.getBestLength() + ",  ");
        System.out.print(" time is :" + aco.getWholeTime());
        System.out.println();

        // int times = 49;
        //
        // while (times >= 1) {
        //     for (int i = 0; i < times; i++) {
        //         aco.run();
        //         System.out.print("No." + i + " route is :" + aco.getBestRoute() + ",  ");
        //         System.out.print("No." + i + " length is :" + aco.getBestLength() + ",  ");
        //         System.out.print("No." + i + " time is :" + aco.getWholeTime());
        //         System.out.println();
        //
        //         ArrayList<Integer> timeList = new ArrayList<>();
        //
        //         if (aco.getWholeTime() > 1000) {
        //             //如果当前解不满足条件，需要删除节点
        //             List<Integer> copyCitys = DeepClone.deepCopy(citys);
        //             //从前往后遍历删除每个节点，找出其中满足条件的最优路径
        //             int curLength = 10000000; //定义一个较大数
        //             int deletedCity = 0;
        //             int curWholeTime = 0;
        //             int curWholeCost = 0;
        //
        //             for (int j = 0; j < times - 1; j++) {
        //                 List<Integer> copyCitys1 = DeepClone.deepCopy(copyCitys);
        //                 copyCitys1.remove(times + 1);
        //                 //重新计算最优路径
        //                 aco = new ACO(copyCitys1, pheromoneMatrix, distanceMatrix,timeMartix, 150, 72);
        //                 //判断是否满足条件
        //                 if (aco.getWholeTime() > 1000) { //当前所删除节点不满足条件
        //                     continue;
        //                 } else { //当前所删除节点满足条件
        //                     if (aco.getBestLength() < curLength) { //如果所删除节点对应的路径长度小于上一个删除节点对应的路径长度，则替换
        //                         deletedCity = j + 1;
        //                         curWholeTime = aco.getWholeTime();
        //                     }
        //                 }
        //                 timeList.add(deletedCity);
        //                 timeList.add(curWholeTime);
        //             }
        //
        //             break;
        //         }
        //         citys.add(i+1);
        //     }
        //
        // }

//
    }

    public static void test1() {
        //自定义距离矩阵
        int[][] distanceMatrix = new int[][]{{0, 1, 3, 1}, {1, 0, 3, 2}, {3, 3, 0, 2}, {1, 2, 2, 0}};
        int[][] timeMatrix = new int[][]{{0, 1, 1, 1}, {1, 0, 4, 5}, {1, 4, 0, 3}, {1, 5, 3, 0}};
        //创建信息素矩阵并赋初值
        double[][] pheromoneMatrix = new double[4][4];
        for(int i=0; i<distanceMatrix.length; i++) {
            for(int j=0; j<distanceMatrix.length; j++) {
                pheromoneMatrix[i][j] = 0.1;
            }
        }
        //创建城市集合
        List<Integer> citys = new ArrayList<Integer>();
        citys.add(1);
        //运行蚁群优化算法
        ACO aco = new ACO(citys, pheromoneMatrix, distanceMatrix, timeMatrix, 50, 6);

        for (int i = 1;;i++) {
            aco.run();
            System.out.print("No." + i + " route is :" + aco.getBestRoute() + ",");
            System.out.print("No." + i + " length is :" + aco.getBestLength() + ",");
            System.out.print("No." + i + " time is :" + aco.getWholeTime());
            System.out.println();
            if (aco.getWholeTime() > 6) {
            break;
        }
                citys.add(i + 1);

        }


    }
}
